<template>
  <div>
    <Header></Header>
    <div class="p-40 button-nav">
      <div class="" style="width: 800px;margin: auto">
        <div class="pt-10 pb-10 pl-20 pr-20 bg-grey flex font-14">
          <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>考生信息</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <div class="bg-shadow mt-20 p-40 bg-white">
          <el-form :model="form" label-width="120px"
                   ref="formRef" :rules="rules"
                   style="width: 50%;margin: auto;">
            <el-form-item label="姓名" prop="username">
              <el-input v-model="form.username" placeholder="请输入姓名"/>
            </el-form-item>
            <el-form-item label="身份证号" prop="id_card">
              <el-input v-model="form.id_card" placeholder="请输入身份证号"/>
            </el-form-item>
            <el-form-item label="所属单位" prop="id_card">
              <el-input v-model="form.unit" placeholder="请输入所属单位"/>
            </el-form-item>
            <el-form-item label="职务" prop="job">
              <el-input v-model="form.job" placeholder="请填写职务全称"/>
            </el-form-item>
            <el-form-item label="联系电话" prop="mobile">
              <el-input v-model="form.mobile" placeholder="请输入联系电话"/>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onSubmit">修改信息</el-button>
              <el-button @click="goBack">返回</el-button>
            </el-form-item>
          </el-form>
          <div class="text-center">
            <el-button type="primary" link @click="goPass">修改密码</el-button>
            <el-button type="" link @click="loginOut">退出登录</el-button>
          </div>
        </div>
      </div>
    </div>
    <Footer></Footer>
  </div>
</template>

<script>
import Header from "@/components/Header";
import Footer from "@/components/Footer";

import {getUserInfo, register, updateUser} from '@/api/api.js'

const rules = {
  username: [
    { required: true, message: '请输入姓名', trigger: 'blur' }
  ],
  id_card: [
    { required: true, message: '请输入身份证号', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ],
  job: [
    { required: true, message: '请填写职务全称', trigger: 'blur' }
  ],
  mobile: [
    { required: true, message: '请输入联系电话', trigger: 'blur' }
  ]
}

export default {
  name: "Register",
  components: {
    Header, Footer
  },
  data() {
    return {
      form: {
        username: '',
        password: '',
        id_card: '',
        unit: '',
        unit_type: '',
        job: '',
        mobile: '',
        type: '',
        sex: 1,
      },
      rules: rules,
      unit_type: ['党政机关', '事业单位', '国有企业', '群团组织', '垂管部门'],
      type: ['厅局级', '县处级', '科级', '股级', '科员', '国企负责人', '国企中层', '其他国家工作人员'],
    }
  },
  mounted() {
    this.getUserInfo()
  },
  methods: {
    async getUserInfo() {
      this.form = await getUserInfo()
    },
    onSubmit() {
      this.$refs.formRef.validate(async (valid) => {
        if (!valid) {
          this.$message.error('请填写或选择所有必填项')
          return false
        }
        // operation code
        const data = await updateUser(this.form)
        console.log('返回',data)
        if (data) {
          this.$message.success('修改成功')
        }
      })
    },
    goBack() {
      this.$router.back()
    },
    goPass() {
      this.$router.push('/user/password')
    },
    loginOut() {
      localStorage.removeItem('xx-token')
      this.$router.push('/')
    }
  }
}
</script>

<style scoped>

</style>
